package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupFormDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    /**
     * 新增规格参数分组
     * @param bpdy 规格参数分组数据
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> insertGroup(@RequestBody SpecGroupFormDTO bpdy)
    {
        specGroupService.insertGroup(bpdy);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数分组数据
     * @param body 规格参数组数据
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupFormDTO body)
    {
        specGroupService.updateGroup(body);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据商品分类id查询规格分组的集合
     * @param cid 商品分类id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupFormDTO>> selectSpeclistByCategoryId(@RequestParam("id") Long cid)
    {

        return ResponseEntity.ok(specGroupService.selectSpeclistByCategoryId(cid));
    }

    /**
     *根据条件查询规格参数集合
     * @param cid 分类id
     * @param gid 分组id
     * @param search 是否搜索
     * @return
     */
    @GetMapping("/param")
    public ResponseEntity<List<SpecParamDTO>> selectParamsByConditions(@RequestParam(value = "categoryId",required = false) Long cid,
                                                                       @RequestParam(value = "groupId",required = false)Long gid,
                                                                       @RequestParam(value = "searching",required = false)Boolean search)
    {

        return ResponseEntity.ok(specParamService.selectParamsByConditions(cid,gid,search));
    }


    /**
     * 新增规格参数数据
     * @param body 规格参数数据
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> insertParam(@RequestBody SpecParamDTO body)
    {
        specParamService.insertParam(body);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数数据
     * @param body 规格参数数据
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO body)
    {
        specParamService.updateParam(body);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid 分类id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupFormDTO>> selctParamsByCategoryId(@RequestParam("id") Long cid)
    {
        return ResponseEntity.ok(specGroupService.selctParamsByCategoryId(cid));
    }



}
